package com.study.zzedu.fragment.tab.discuss;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.study.zzedu.widget.bean.TreeNode;
import com.study.zzedu.R;
import com.study.zzedu.adapter.DiscussAdapter;
import com.study.zzedu.base.BaseFragment;
import com.study.zzedu.entity.DiscussItem;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kiros on 2020/9/5.
 * 课程页面
 */

public class DiscussFragment extends BaseFragment<DiscussContract.IView, DiscussPresenter> implements DiscussContract.IView {
    private RecyclerView.LayoutManager layoutManager;

    private RecyclerView rl_discuss;
    private List<DiscussItem> discussItems = new ArrayList<>();
    private DiscussAdapter discussAdapter;

    @Override
    protected DiscussPresenter createPresenter() {
        return new DiscussPresenter();
    }

    @Override
    protected DiscussContract.IView createView() {
        return this;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_discuss;
    }

    protected void initView(View view) {
        rl_discuss = view.findViewById(R.id.rl_discuss);
        layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        discussAdapter = new DiscussAdapter(getContext(), discussItems);
        rl_discuss.setLayoutManager(layoutManager);
        rl_discuss.setAdapter(discussAdapter);
        //添加空  header
        discussItems.add(new DiscussItem("", 0, "", "", "", true, 0));
    }

    @Override
    protected void initData() {
        getPresenter().queryPostList(getContext(), AppConstant.coursewareCode);
    }

    @Override
    protected void initListener() {
    }

    @Override
    public void getCourseContentData(String data) {
        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray jArray = jObject.getJSONArray("data");
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject job = jArray.getJSONObject(i);
                discussItems.add(new DiscussItem(
                        job.getString("id"),
                        job.getInt("views"),
                        job.getString("title"),
                        job.getString("pubdate"),
                        "",
                        job.getBoolean("top"),
                        job.getInt("comments")
                ));
            }
            if ((jArray.length() > 0)) {
                getActivity().findViewById(R.id.rl_default_discass).setVisibility(View.GONE);
                discussAdapter.notifyDataSetChanged();
            }else {
                return;
            }
        } catch (Exception e) {
            e.toString();
        }
    }

    @Override
    public void getData(String data) {

    }

    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }
}
